package org.budo.time.impl;

import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.budo.time.TimePoint;

import java.util.Calendar;
import java.util.Date;

/**
 * @author lmw
 */
@Slf4j
@NoArgsConstructor
public class BudoTimePoint extends AbstractTimePoint {
    private static final long serialVersionUID = 8227187159610051289L;

    public BudoTimePoint(Date date) {
        super(date);
    }

    /**
     * 时间计算，采用Calendar实现
     */
    public TimePoint timeFieldPlus(Integer field, Number amount) {
        if (null == this.getDate()) {
            return new BudoTimePoint(null);
        }

        Calendar calendar = this.toCalendar();

        if (null != amount) {
            calendar.add(field, amount.intValue());
        } else {
            log.error("#35 this=" + this + ", field=" + field + ", amount=" + amount);
        }

        return new BudoTimePoint(calendar.getTime());
    }

    public TimePoint timeFieldSet(Integer field, Number value) {
        if (null == this.getDate()) {
            return new BudoTimePoint(null);
        }

        Calendar calendar = this.toCalendar();

        if (null != value) {
            calendar.set(field, value.intValue());
        } else {
            // log.error("#51 this=" + this + ", field=" + field + ", value=" + value);
        }

        return new BudoTimePoint(calendar.getTime());
    }
}
